package org.nmgyjt.springboot.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class NoSensitiveUtils {

    public static String desIdCardForString(String str) {
        try {
            Pattern IdCardP = Pattern.compile("[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]");//正则表达式

            Matcher m = IdCardP.matcher(str);
            String idCard = "";
            while (m.find()) {
                idCard = m.group(0);
                String s = SensitiveInfoUtils.idCardNum(idCard);
                str = str.replaceAll(idCard, s);
            }
        } catch (Exception e) {
            log.error("脱敏-含有身份证的字符串异常，IdCardStr：{}", str, e);
        }
        return str;
    }


    public static String desMobileForString(String str) {
        try {
            Pattern mobileP = Pattern.compile("(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}");

            String mobile = "";
            Matcher m = mobileP.matcher(str);
            while (m.find()) {
                mobile = m.group(0);
                str = str.replaceAll(mobile, mobile.replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        } catch (Exception e) {
            log.error("脱敏-含有手机号码的字符串异常，mobileStr：{}", str, e);
        }
        return str;
    }

}
